package com.mofeng.myshop.service;


import com.mofeng.myshop.domain.vo.user.UserVo;
import com.mofeng.myshop.service.user.UserUtilsService;
import com.mofeng.myshop.until.Constants;
import com.mofeng.myshop.until.LoginUserThreadLocalUtil;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * 权限表
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class PermissionService {

    private final UserUtilsService userUtilsService;

    private final int ROOT = Constants.User.ROLE_ROOT;
    private final int ADMIN = Constants.User.ROLE_ADMIN;
    private final int MASTER = Constants.User.ROLE_MASTER;
    private final int NORMAL = Constants.User.ROLE_NORMAL;
    private final int USER = Constants.User.ROLE_USER;

    /**
     * 获取登录用户
     */
    private UserVo getUser() {
        UserVo userVo = LoginUserThreadLocalUtil.get();
        if (userVo == null) {
            userVo = userUtilsService.parseToken();
            LoginUserThreadLocalUtil.set(userVo);
        }
        return userVo;
    }

    /**
     * 判断是不是次级管理员及以上权限
     */
    public boolean ADMIN() {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkPower(userByToken, ADMIN);
    }

    /**
     * 判断是不是管理员及以上权限
     */
    public boolean MASTER() {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkPower(userByToken, MASTER);
    }

    /**
     * 判断是不是Root用户
     */
    public boolean ROOT() {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkPower(userByToken, ROOT) || "1912383953@qq.com".equals(userByToken.getEmail());
    }

    /**
     * 普通用户及以上权限
     */
    public boolean NORMAL() {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkPower(userByToken, NORMAL);
    }

    /**
     * 用户自己
     */
    public boolean own(String checkId) {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkOwn(userByToken, checkId);
    }

    /**
     * 用户自己或Root用户
     */
    public boolean ownOrRoot(String checkId) {
        UserVo userByToken = getUser();
        if (userByToken == null) {
            return false;
        }
        return checkOwn(userByToken, checkId) || checkPower(userByToken, ROOT);
    }

    /**
     * 判断自己权限权限是否大于当前权限
     *
     * @return true 是
     */
    public boolean checkHighOwnPower(Integer power) {
        if (power == null){
            return false;
        }
        UserVo user = getUser();
        int powerThis = user.getPowerNumber();
        return powerThis > power;
    }

    /**
     * 判断自己权限权限是否大于等于当前权限
     *
     * @return true 是
     */
    public boolean checkHighEqOwnPower(Integer power) {
        if (power == null){
            return false;
        }
        UserVo user = getUser();
        int powerThis = user.getPowerNumber();
        return powerThis >= power;
    }

    /**
     * 判断权限是否ROOT
     *
     * @return true 是
     */
    public boolean checkIsRoot(Integer power) {
        if (power == null){
            return false;
        }
        return ROOT == power;
    }

    /**
     * 判断权限是否游客
     *
     * @return true 是
     */
    public boolean checkIsUSER(Integer power) {
        if (power == null){
            return false;
        }
        return USER == power;
    }

    /**
     * 判断权限
     *
     * @return true 是
     */
    private boolean checkPower(UserVo user, Integer power) {
        if (power == null){
            return false;
        }
        int powerThis = user.getPowerNumber();
        return powerThis >= power;
    }

    /**
     * 判断是不是用户自己
     */
    private boolean checkOwn(UserVo loginUser, String checkId) {
        return loginUser.getId().equals(checkId);
    }

//    /**
//     * 判断是不是管理员及以上权限
//     *
//     * @return true 是
//     */
//    private boolean checkAdmin(BlogUser user) {
//        int power = Integer.parseInt(user.getUserPower().getPower());
//        return power >= ADMIN;
//    }
//
//    /**
//     * 判断是不是博主及以上权限
//     *
//     * @return true 是
//     */
//    private boolean checkMaster(BlogUser user) {
//        int power = Integer.parseInt(user.getUserPower().getPower());
//        return power >= MASTER;
//    }
//
//    /**
//     * 判断是不是最高权限
//     *
//     * @return true 是
//     */
//    private boolean checkRoot(BlogUser user) {
//        int power = Integer.parseInt(user.getUserPower().getPower());
//        return power >= ROOT;
//    }
//    /**
//     * 判断是不是管理员及以上权限
//     *
//     * @return true 是
//     */
//    private boolean checkNormal(BlogUser user) {
//        int power = Integer.parseInt(user.getUserPower().getPower());
//        return power >= NORMAL;
//    }


//    /**
//     * 判断是不是普通用户及以上权限
//     *
//     * @param user
//     * @return
//     */
//    private boolean checkNormalUser(BlogUser user) {
//        int power = Integer.parseInt(user.getUserPower().getPower());
//        return power >= NORMAL;
//    }
}

//@Getter
//@AllArgsConstructor
//enum Power {
//    USER(Integer.parseInt(Constants.User.ROLE_USER)),
//    ROOT(Integer.parseInt(Constants.User.ROLE_ROOT)),
//    ADMIN(Integer.parseInt(Constants.User.ROLE_ADMIN)),
//    MASTER(Integer.parseInt(Constants.User.ROLE_MASTER)),
//    NORMAL(Integer.parseInt(Constants.User.ROLE_NORMAL)),
//    ;
//
//    private final Integer power;
//
//
//}